package com.foogui.primary.communication;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    private static final int NUMBER=5;
    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(NUMBER, () -> {
            System.out.println("所有的线程都到达栅栏处了");
        });
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"号线程到达了栅栏");
                try {
                    barrier.await();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"号线程越过栅栏，解除阻塞，继续执行");
            },i+"").start();
        }

    }
}
